﻿@{
	ViewBag.Title = "角色列表";
}

@section header{
	<style>
		.fast-filter .tablink {
			padding-right: 8px;
			padding-left: 2px;
		}
	</style>
}
@section quickmenu{
	<td style="padding-right: 0px; padding-left: 10px;">
		<span style="cursor: pointer;" title="新建..." @@click="listAdd">
			<i class="icon-plus icon-2x pull-left icon-border crm-bu-style"></i>
		</span>
	</td>
	<td style="padding-right: 0px; padding-left: 10px;">
		<span style="cursor: pointer;" title="查找..." @@click="searchToggle">
			<i class="icon-search icon-2x pull-left icon-border crm-bu-style"></i>
		</span>
	</td>
}

@*页面视图列表开始*@
<table class="list_table table100 fast-filter">
	<tbody>
		<tr>
			<td style="">
				<i class="icon-filter pure-icon-bu"></i><span>
					显示：<a class="cus_markbai tablink" href="javascript:void(0);" @@click="searchReset">全部</a>
				</span>
				@*<i class="icon-filter pure-icon-bu"></i><span>
						状态：<a class="cus_markbai tablink" href="javascript:void(0);" @@click="listFastFilter($event)" data-ftype="reset" data-fname="donestate">不限</a>
						<a class="cus_markhui tablink" href="javascript:void(0);" @@click="listFastFilter($event)" data-fname="donestate" data-fvalue="@((int)BillState.未结算)">待结算</a>
						<a class="cus_markhui tablink" href="javascript:void(0);" @@click="listFastFilter($event)" data-fname="donestate" data-fvalue="@((int)BillState.待付款)">已结算</a>
					</span>
					<i class="icon-filter pure-icon-bu"></i>
					<span>
						时间：<a class="cus_markbai tablink" href="javascript:void(0);" @@click="listFastFilter($event)" data-ftype="reset" data-fstart="studentdate_s" data-fend="studentdate_e">不限</a>
						<a class="cus_markhui tablink" href="javascript:void(0);" @@click="listFastFilter($event)" data-ftype="date" data-fstart="studentdate_s" data-fend="studentdate_e" data-fvalue="nowd">当天</a>
						<a class="cus_markhui tablink" href="javascript:void(0);" @@click="listFastFilter($event)" data-ftype="date" data-fstart="studentdate_s" data-fend="studentdate_e" data-fvalue="noww">本周</a>
						<a class="cus_markhui tablink" href="javascript:void(0);" @@click="listFastFilter($event)" data-ftype="date" data-fstart="studentdate_s" data-fend="studentdate_e" data-fvalue="nowm">本月</a>
						<a class="cus_markhui tablink" href="javascript:void(0);" @@click="listFastFilter($event)" data-ftype="date" data-fstart="studentdate_s" data-fend="studentdate_e" data-fvalue="nowy">本年</a>
						<a class="cus_markhui tablink" href="javascript:void(0);" @@click="listFastFilter($event)" data-ftype="date" data-fstart="studentdate_s" data-fend="studentdate_e" data-fvalue="lastw">上周</a>
						<a class="cus_markhui tablink" href="javascript:void(0);" @@click="listFastFilter($event)" data-ftype="date" data-fstart="studentdate_s" data-fend="studentdate_e" data-fvalue="lastm">上月</a>
					</span>*@
			</td>
		</tr>
	</tbody>
</table>
@*页面视图列表结束*@

<div id="searchDiv" style="z-index: 1; display: block; position: relative; margin-top: 3px;" v-show="searchShow">
	<table cellpadding="5" cellspacing="2" class="searchUIBasic padding2" align="center" border="0" style="width:90%;">
		<tbody>
			<tr>
				<td class="small" nowrap="" align="center">
					@*<span>学校</span>
						<input type="text" class="txtBox" style="width: 230px" v-model.trim="listConditions.oname">
						<button class="pure-button pure-button-share pure-icon-bu" style="width:23px;height:21px;" onclick="openPopOrg(); return false;">
							<i class="icon-search"></i>
						</button>
						<button class="pure-button pure-button-approve pure-icon-bu" style="width: 23px; height: 21px;" title="清除" onclick="clearPopOrg();return false;">
							<i class="icon-remove"></i>
						</button>*@
					<span>角色名称</span>
					<input type="text" class="txtBox" style="width: 100px" v-model.trim="listConditions.name">
					<span>编码</span>
					<input type="text" class="txtBox" style="width: 130px" v-model.trim="listConditions.code">
					<span>状态</span>
					@Html.DropDownList("state", ViewBag.StateOptions as IEnumerable<SelectListItem>, "不限", new { v_model = "listConditions.state" })
					@*<span>出生日期</span>
						<input name="birth_s" type="text" class="calendar" id="birth_s" style="width: 70px;" maxlength="19" v-model.trim="listConditions.birth_s" />
						<i class="icon-calendar calendar-tgr" id="birth_s_tgr"></i>&nbsp;-&nbsp;
						<input name="birth_e" type="text" class="calendar" id="birth_e" style="width: 70px;" maxlength="19" v-model.trim="listConditions.birth_e" />
						<i class="icon-calendar calendar-tgr" id="birth_e_tgr"></i>*@
					&nbsp;
					<button class="pure-button pure-button-edit" title="查找" @@click.prevent="listRefresh(1)">
						<i class="icon-search"></i>查找
					</button>
					&nbsp;
					<button class="pure-button pure-button-cancel" title="取消" @@click.prevent="searchReset">
						<i class="icon-rotate-left"></i>重置
					</button>
					&nbsp;&nbsp;
				</td>
				<td class="small" valign="top">
					<input class="windLayerBtclose" type="button" title="" @@click="searchToggle">
				</td>
			</tr>
		</tbody>
	</table>
</div>
<table class="lvt small table100" style="">
	<tr>
		<td style="padding-left: 30px;" align="left" nowrap>
			<button class="pure-button pure-button-edit" @@click.prevent="listAdd">
				<i class="icon-plus"></i>&nbsp;新增
			</button>
			&nbsp;
			<button class="pure-button pure-button-danger" @@click.prevent="listBatchDelete">
				<i class="icon-trash"></i>&nbsp;批量删除
			</button>
		</td>
		<td nowrap align="right" valign="middle">
			<table border="0" cellspacing="0" cellpadding="0" class="small">
				<tbody>
					<tr>
						<td style="padding-right: 5px">
							<vue-pagination :total="listData.total" :psize="listData.psize" :pnum="listData.pnum" @@change="pageChangeHandler"></vue-pagination>
						</td>
					</tr>
				</tbody>
			</table>
		</td>
	</tr>
</table>
<div style="width:100%;overflow:auto;">
	<table class="crm-table table100 padding2" cellspacing="1" cellpadding="3" style="table-layout:fixed;">
		<caption v-show="listData.list.length==0">
			暂无数据
		</caption>
		<tr class="windLayerHead">
			<td style="width: 30px;text-align:center;">
				<input type="checkbox" name="selectall" @@click="listToggle($event)" v-model="listData.checked" />
			</td>
			@*<td style="width: 30px;">状态</td>*@
			<td v-for="(col,idx) in listColumns" :style="col.hdStyles">
				<a class='listFormHeaderLinks' @@click="listSort(col.name)" v-if="col.sort">{{col.title}}<i :class="{'icon-caret-up':listSortColumn.order=='asc','icon-caret-down':listSortColumn.order=='desc'}" v-if="col.name==listSortColumn.name"></i></a>
				<span v-else>{{col.title}}</span>
			</td>
			<td nowrap style="width:60px;">
				操作
			</td>
		</tr>
		<tr v-for="(item,idx) in pageList" :rid="item.ID" :class="{'crm-table-plural':idx%2==1}" style="cursor: pointer;">
			<td style="text-align:center;">
				<input type="checkbox" name="rowid" :value='item.ID' :data-idx="idx" @@click="listCheck($event)" :checked="item.CheckedState" />
			</td>
			@*<td style="text-align:center;">
					<div style="border-radius:50%;width:12px;height:12px;margin:0 auto;" :style="item.Style" :title="item.StateNowText"></div>
				</td>*@
			<td v-for="(col,cidx) in listColumns" :style="item.Styles[cidx]">
				<a :href="item.Links[cidx].href" v-if="item.Links[cidx].href">{{item[col.name]}}</a>
				<span v-else-if="col.format" v-html="col.format(item[col.name],item)"></span>
				<span v-else>{{item[col.name]}}</span>
			</td>
			<td nowrap>
				<i class="icon-edit pure-icon-bu crm-bu-style list-action-btn" title='修改' style="" @@click="listEdit(item)"></i>
				<i class="icon-trash pure-icon-bu crm-bu-style list-action-btn" title='删除' style="" @@click="listDelete(item)"></i>
			</td>
		</tr>
	</table>
</div>
@*生成列表操作*@
@section footer{
	@*日期必须开始*@
	<script src="~/lib/laydate/laydate.js" asp-append-version="true"></script>
	<script src="~/lib/moment/moment.min.js" asp-append-version="true"></script>
	@*日期必须结束*@
	<script type="text/javascript">
		@* 列表相关开始 *@
		var listConditionsInit = { state: "" };//初始查询条件
		var pageEnumData = {
			state:@(Json.Serialize(ViewBag.StateOptions)),
		};
		var pageData = {
			hideBreadcrumb: false,
			serviceCode: '@(ViewBag.ServiceCode)',
			global: globalData,
			listData: {
				checked:false,
				list: [],
				total: 0,
				pnum: 1,
				psize: 20,
				sumcol: [],
				listsumtotal: [],
				pagesumtotal: []
			},
			listColumns: [
				{
					title: "角色名称",
					name: "Name",
					sort: true,
					href:"@(Url.Action("ViewDetail", "Sys_Role"))/{ID}",
					hdStyles: {
						width: "130px",
					},
					bdStyles: {
					}
				},
				{
					title: "角色编码",
					name: "Code",
					sort: true,
					href:"@(Url.Action("ViewDetail", "Sys_Role"))/{ID}",
					hdStyles: {
						width: "80px",
					},
					bdStyles: {
					}
				},
				{
					title: "角色说明",
					name: "Comment",
					hdStyles: {
						width: "260px",
					},
					bdStyles: {
					}
				},
				{
					title: "排序值",
					name: "Sort",
					hdStyles: {
						width: "60px",
						textAlign:'center',
					},
					bdStyles: {
						textAlign:'center',
					}
				},
				{
					title: "状态",
					name: "StateNow",
					hdStyles: {
						width: "60px",
						textAlign:'center',
					},
					bdStyles: {
						textAlign:'center',
					}
				},
				{
					title: "是否系统保留",
					name: "Reserved",
					hdStyles: {
						width: "60px",
						textAlign:'center',
					},
					bdStyles: {
						textAlign:'center',
					},
					format: function (val, item) {
						return val?"<i class='icon-check'></i>":"";
					}
				},
				{
					title: "是否允许修改",
					name: "Freezed",
					hdStyles: {
						width: "60px",
						textAlign:'center',
					},
					bdStyles: {
						textAlign:'center',
					},
					format: function (val, item) {
						return !val?"<i class='icon-check'></i>":"";
					}
				},
				{
					title: "下级是否可见",
					name: "ChildVisible",
					hdStyles: {
						width: "60px",
						textAlign:'center',
					},
					bdStyles: {
						textAlign:'center',
					},
					format: function (val, item) {
						return val?"<i class='icon-check'></i>":"";
					}
				},
				{
					title: "创建日期",
					name: "AddDate",
					hdStyles: {
						width: "120px",
					},
					bdStyles: {
					}
				},
				{
					title: "创建人",
					name: "AddManName",
					hdStyles: {
						width: "60px",
					},
					bdStyles: {
					}
				},
			],
			searchShow:true,
			listConditions: Object.assign({}, listConditionsInit),
			listSortColumn: {
				name: "",
				order:""
			},
			listStyles: {
				@*"StateNow": {
					"@((int)ST.Buz.Base.Enums.BillState.已付款)": { backgroundColor:"#FFCC00"},
					"@((int)ST.Buz.Base.Enums.BillState.已作废)": { backgroundColor:"#FF0000"},
					"@((int)ST.Buz.Base.Enums.BillState.已删除)": { backgroundColor:"#cc4646"},
					"@((int)ST.Buz.Base.Enums.BillState.已退款)": { backgroundColor:"#E2AF08"},
					"@((int)ST.Buz.Base.Enums.BillState.待付款)": { backgroundColor:"#009963"},
					"@((int)ST.Buz.Base.Enums.BillState.未结算)": { },
				}*@
			},
			listColStyles: {
			},
			model: {},
		};

		var pageApp = new Vue({
			el: '#app',
			data: pageData,
			mounted: function () {
				this.listConditions = Object.assign({},this.listConditions, utils.pageRestore());
				this.listRefresh();
			},
			methods: {
				pageChangeHandler: function (args) {
					this.listRefresh(args.pnum, args.psize)
				},
				listFastFilter: function (e) {
					var obj = e.srcElement || e.target;
					$(obj).parent('span').find('.tablink').removeClass('cus_markbai').addClass('cus_markhui');
					$(obj).addClass('cus_markbai');
					switch (obj.dataset.ftype) {
						case "date":
							{
								if (!obj.dataset.fstart || !obj.dataset.fend) {
									return;
								}
								var dts = '';
								var dte = '';
								switch (obj.dataset.fvalue) {
									case "nowd":
										dts = moment().startOf('day').format('YYYY-MM-DD');
										dte = moment().format('YYYY-MM-DD');
										break;
									case "noww":
										dts = moment().startOf('isoWeek').format('YYYY-MM-DD');
										dte = moment().format('YYYY-MM-DD');
										break;
									case "nowm":
										dts = moment().startOf('month').format('YYYY-MM-DD');
										dte = moment().format('YYYY-MM-DD');
										break;
									case "nowy":
										dts = moment().startOf('year').format('YYYY-MM-DD');
										dte = moment().format('YYYY-MM-DD');
										break;
									case "lastw":
										dts = moment().week(moment().week() - 1).startOf('isoWeek').format('YYYY-MM-DD');
										dte = moment().week(moment().week() - 1).endOf('isoWeek').format('YYYY-MM-DD');
										break;
									case "lastm":
										dts = moment().month(moment().month() - 1).startOf('month').format('YYYY-MM-DD');
										dte = moment().month(moment().month() - 1).endOf('month').format('YYYY-MM-DD');
										break;
								}
								var dates = {};
								dates[obj.dataset.fstart] = dts;
								dates[obj.dataset.fend] = dte;
								this.listConditions = Object.assign({}, this.listConditions, dates);
								this.listRefresh(1);
							}
							break;
						case "reset":
							{
								var values = {};
								if (obj.dataset.fname) {
									values[obj.dataset.fname] = obj.dataset.fvalue;
									this.listConditions = Object.assign({}, this.listConditions, values);
								} else if (obj.dataset.fstart && obj.dataset.fend) {
									values[obj.dataset.fstart] = '';
									values[obj.dataset.fend] = '';
									this.listConditions = Object.assign({}, this.listConditions, values);
								}
								this.listRefresh(1);
							}
							break;
						default:
							{
								if (!obj.dataset.fname) {
									return;
								}
								var values = {};
								values[obj.dataset.fname] = obj.dataset.fvalue;
								this.listConditions = Object.assign({}, this.listConditions, values);
								this.listRefresh(1);
							}
					}
				},
				listRefresh: function (pageNum, pageSize) {
					var _params = Object.assign({}, {
						pnum: pageNum || this.listData.pnum,
						psize: pageSize || this.listData.psize,
						sortorder: !this.listSortColumn.name ? "" : this.listSortColumn.name + "_" + this.listSortColumn.order
					}, this.listConditions);
					utils.pageStore(_params,pageNum,pageSize);
					var _this = this;
					utils.ajaxPost('@(Url.Action("Search", "Sys_Role"))', _params, function (resp) {
						var list = resp.data.list.map(function (item) {
							return Object.assign({}, item, {
								CheckedState: false,
							});
						});
						var sumcol = resp.data.sumcol || [];
						var sumtotal = resp.data.sumtotal || [];
						var pagesumtotal = sumcol.map(function (col) {
								return resp.data.list.reduce(function (total, cur) {
									return total + cur[col];
								}, 0);
							});
						var newData = { sumcol: sumcol, listsumtotal: sumtotal, pagesumtotal: pagesumtotal,list: list, total: resp.data.total, pnum: resp.data.pnum, psize: resp.data.psize ,checked:false};
						_this.listData = newData;
					}, function (err) {
					})
				},
				searchReset: function () {
					$('.fast-filter').find('.tablink[data-ftype="reset"]').each(function (idx, elem) {
						$(elem).parent('span').find('.tablink').removeClass('cus_markbai').addClass('cus_markhui');
						$(elem).removeClass('cus_markhui').addClass('cus_markbai');
					});
					this.listConditions = Object.assign({}, listConditionsInit);
					this.listSortColumn = {
						col: "",
						order: ""
					};
					this.listRefresh(1);
				},
				searchToggle: function () {
					this.searchShow = !this.searchShow;
				},
				listSort: function (col) {
					if (col != this.listSortColumn.name) {
						this.listSortColumn.order = "desc";
						this.listSortColumn.name = col;
					} else {
						this.listSortColumn.order = this.listSortColumn.order == "asc" ? "desc" : "asc";
					}
					this.listRefresh(1);
				},
				listToggle: function (e) {
					var checked = (e.srcElement || e.target).checked;
					this.listData.list=this.listData.list.map(function (item) {
						return Object.assign({}, item,{
							CheckedState: checked
						});
					});
				},
				listCheck: function (e) {
					var obj = e.srcElement || e.target;
					var checked = obj.checked;
					this.listData.list.splice(obj.dataset.idx, 1, Object.assign({}, this.listData.list[obj.dataset.idx], {
						CheckedState: checked
					}));
				},
				listAdd: function () {
					location.href = '@(Url.Action("Add", "Sys_Role"))?nc=' + new Date().getTime();
				},
				listEdit: function (item) {
					location.href = '@(Url.Action("Edit", "Sys_Role"))/' + item.ID + '?nc=' + new Date().getTime();
				},
				listDelete: function (item) {
					var _this = this;
					utils.confirm('确定要删除角色【' + item.Name +'】？', function () {
						utils.ajaxPost('@(Url.Action("Delete","Sys_Role"))', { id: item.ID }, function (resp) {
							utils.success("删除成功");
							_this.listRefresh();
						}, function (msg) {
							utils.alert(msg);
						});
					});
				},
				listBatchDelete: function () {
					var selected = this.listData.list.filter(function (item) {
						return item.CheckedState;
					});
					var ids = selected.map(function (item) {
						return item.ID;
					});
					if (!ids||ids.length == 0) {
						utils.alert('请先选择要删除的角色');
						return;
					}
					var _this = this;
					utils.confirm('确定要删除所选择的角色？', function () {
						utils.ajaxPost('@(Url.Action("BatchDelete", "Sys_Role"))',  ids , function (resp) {
							utils.success("删除成功");
							_this.listRefresh();
						}, function (msg) {
							utils.alert(msg);
						});
					});
				}
			},
			computed: {
				pageList: function () {
					var _this = this;
					return this.listData.list.map(function (item) {
						return Object.assign({}, item, {
							StateNow: _this.$options.filters.formatState(item.StateNow),
							Styles: _this.listColumns.map(function (col) {//列样式
								return Object.assign({}, col.bdStyles, _this.listColStyles[col.name] ? _this.listColStyles[col.name][item[col.name]] : {})
							}),
							Style: {},
							//Style: _this.listStyles.StateNow[item.StateNow],//行样式
							Links: _this.listColumns.map(function (col) {
								return Object.assign({}, { href: col.href ? col.href.replace(/{ID}/g, item.ID) : "" });
							}),
						});
					});
				},
				currentM: function () {
					var currentM = null;
					var i;
					var j;
					for (i = 0; i < this.global.modules.length; i++) {
						for (j = 0; j < this.global.modules[i].Services.length; j++) {
							if (this.serviceCode == this.global.modules[i].Services[j].Code) {
								currentM = this.global.modules[i];
								break;
							}
						}
					}
					return currentM;
				},
				currentS: function () {
					var currentS = null;
					var i;
					var j;
					for (i = 0; i < this.global.modules.length; i++) {
						for (j = 0; j < this.global.modules[i].Services.length; j++) {
							if (this.serviceCode == this.global.modules[i].Services[j].Code) {
								currentS = this.global.modules[i].Services[j];
								break;
							}
						}
					}
					return currentS;
				}
			},
			filters: {
				formatState: function (val) {
					return utils.formatEnum(val, pageEnumData.state);
				}
			}
		});
		@* 列表相关结束 *@
	</script>
}
